package com.example.jack.community.view.dynamic;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jack.community.R;
import com.example.jack.community.view.dynamic.dynamic_bean.CustomAnim;
import com.example.jack.community.view.dynamic.dynamic_data.activityDATASGson;

import cn.pedant.SweetAlert.SweetAlertDialog;


public class DynamicActivity extends AppCompatActivity implements View.OnClickListener {

    private CollapsingToolbarLayoutState state;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private ButtonBarLayout playButton;
    private AppBarLayout app_bar;

    String number;
    activityDATASGson activitydataclass;

    String conttent;
    String attentionImageViewnum = "0";
    CustomAnim ca;

    private CoordinatorLayout attentionButton, activityComment, eventSharing;

    String coverPicture1, clubLogo1, clubName1, clubAbout1, Toolbar_clubName1, aactivityName1, activityTime1,
            aactivityPlace1, activityMatter1, activityRemark1;

    private ImageView coverPicture, clubLogo, attentionImageView;
    private TextView clubName, clubAbout, Toolbar_clubName;
    private Button activityButton;
    private TextView aactivityName, activityTime, aactivityPlace, activityMatter, activityRemark, activityEssay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);

        //获取上一界面返回的数据
        //取得从上一个Activity当中传递过来的Intent对象
        Intent intent = getIntent();
        //从Intent当中根据key取得value
//        number = intent.getStringExtra("number");
//        Log.e("123123123", number);

//        initjudge();//数据解析并拿出来
        initView();//绑定控件

        ca = new CustomAnim();//绑定Button动画Class

        intApp_bar();//CollapsingToolbarLayout的状态并实现相应的逻辑。
        initData();//绑定数据上去
        initClick();//点击事件
    }

    //数据解析并拿出来
//    private void initjudge() {
//        Gson gson = new Gson();
//        activitydataclass = gson.fromJson(number,activityDATASGson.class);
//        coverPicture1 = StateJudgment.PictureRequest(activitydataclass.getPoster());
//        clubLogo1 = StateJudgment.PictureRequest(activitydataclass.getCommunityLogo());
//        clubName1 = activitydataclass.getCommunityName();
//        clubAbout1 = activitydataclass.getActivitiesName();
//        Toolbar_clubName1 = activitydataclass.getCommunityName();
//        aactivityName1 = activitydataclass.getActivitiesName();
//        activityTime1 = "时间："+ DateUtils.getTimeConversionDate(activitydataclass.getStartActivitiesTime(),
//                activitydataclass.getEndActivitiesTime());
//        aactivityPlace1 = "地点："+ activitydataclass.getPlace();
//        activityMatter1 = "内容："+ activitydataclass.getIntroduction();
//        activityRemark1 = "备注："+ activitydataclass.getRemarks();
//    }


    //点击事件
    private void initClick() {
        activityButton.setOnClickListener(this);
        attentionButton.setOnClickListener(this);
        activityComment.setOnClickListener(this);
        eventSharing.setOnClickListener(this);
    }

    //绑定数据上去
    private void initData() {
        Glide.with(this).load(coverPicture1).into(coverPicture);//在设配器器图片请求
        Glide.with(this).load(clubLogo1).into(clubLogo);
        clubName.setText(clubName1);
        clubAbout.setText(clubAbout1);
        Toolbar_clubName.setText(Toolbar_clubName1);
        aactivityName.setText(aactivityName1);
        activityTime.setText(activityTime1);
        aactivityPlace.setText(aactivityPlace1);
        activityMatter.setText(activityMatter1);
        activityRemark.setText(activityRemark1);
//        activityEssay.setVisibility(View.GONE);

    }

    //绑定控件
    private void initView() {
        coverPicture = (ImageView) findViewById(R.id.activity_coverPicture);//活动海报
        clubLogo = (ImageView) findViewById(R.id.activity_clubLogo);//社团Logo
        clubName = (TextView) findViewById(R.id.activity_clubName);//社团名称
        clubAbout = (TextView) findViewById(R.id.activity_clubAbout);//社团简介
        Toolbar_clubName = (TextView) findViewById(R.id.activity_Toolbar_clubName);//Toolbar上社团名称
        aactivityName = (TextView) findViewById(R.id.activity_activityName);//活动名称
        activityTime = (TextView) findViewById(R.id.activity_activityTime);//活动时间
        aactivityPlace = (TextView) findViewById(R.id.activity_activityPlace);//活动地点
        activityMatter = (TextView) findViewById(R.id.activity_activityMatter);//活动内容
        activityRemark = (TextView) findViewById(R.id.activity_activityRemark);//活动备注
        activityEssay = (TextView) findViewById(R.id.activity_activityEssay);//活动原文链接
        activityButton = (Button) findViewById(R.id.activity_activityButton);//报名按钮
        attentionButton = (CoordinatorLayout) findViewById(R.id.activity_attentionButton);//关注活动按钮
        activityComment = (CoordinatorLayout) findViewById(R.id.activity_activityComment);//活动评论
        eventSharing = (CoordinatorLayout) findViewById(R.id.activity_eventSharing);//活动分享
        attentionImageView = (ImageView) findViewById(R.id.activity_attentionImageView);//关注活动按钮图片
        app_bar = (AppBarLayout) findViewById(R.id.app_bar);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        playButton = (ButtonBarLayout) findViewById(R.id.playButton);
    }


    //CollapsingToolbarLayout的状态并实现相应的逻辑。
    private void intApp_bar() {
        app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    if (state != CollapsingToolbarLayoutState.EXPANDED) {
                        state = CollapsingToolbarLayoutState.EXPANDED;//修改状态标记为展开
//                        collapsingToolbarLayout.setTitle("EXPANDED");//设置title为EXPANDED
                    }
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    if (state != CollapsingToolbarLayoutState.COLLAPSED) {
                        collapsingToolbarLayout.setTitle("");//设置title不显示
                        playButton.setVisibility(View.VISIBLE);//隐藏播放按钮
                        state = CollapsingToolbarLayoutState.COLLAPSED;//修改状态标记为折叠
                    }
                } else {
                    if (state != CollapsingToolbarLayoutState.INTERNEDIATE) {
                        if (state == CollapsingToolbarLayoutState.COLLAPSED) {
                            playButton.setVisibility(View.GONE);//由折叠变为中间状态时隐藏播放按钮
                        }
//                        collapsingToolbarLayout.setTitle("INTERNEDIATE");//设置title为INTERNEDIATE
                        state = CollapsingToolbarLayoutState.INTERNEDIATE;//修改状态标记为中间
                    }
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_activityButton:
                //判断文字进行动画效果
                conttent = "";
                conttent = activityButton.getText().toString().intern();
                //Button文字是点击报名时候
                if (conttent.equals("点击报名")) {
                    //SweetAlertDialog嵌套使用--第一次
                    new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                            .setTitleText("Are you sure participate in?")//标题
                            .setContentText("Will get your information registration!")//内容
                            .setConfirmText("Yes,I Absolutely!")//按钮文字
                            //SweetAlertDialog嵌套使用--第二次
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    sDialog
                                            .setTitleText("Success!")//标题
                                            .setContentText("Successful registration!")//内容
                                            .setConfirmText("OK")//按钮文字
                                            .setConfirmClickListener(null)
                                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                                }
                            })
                            .show();
                    //点击后Button动画改变
                    ca.setDuration(1000);//时间
                    v.startAnimation(ca);
                    activityButton.setTextColor(Color.parseColor("#f25d5d"));//颜色改变
                    activityButton.setText("报名成功");//文字改变
                    break;
                }
                //Button文字是报名成功时候
                if (conttent.equals("报名成功")) {
                    new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("Are you sure you give up??")
                            .setContentText("Your information registration will be cancelled!")
                            .setConfirmText("Yes,I Absolutely!")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    sDialog
                                            .setTitleText("Success!")
                                            .setContentText("Cancellation of registration!")
                                            .setConfirmText("OK")
                                            .setConfirmClickListener(null)
                                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                                }
                            })
                            .show();
                    ca.setDuration(1000);
                    v.startAnimation(ca);
                    activityButton.setTextColor(Color.parseColor("#ffffff"));
                    activityButton.setText("点击报名");
                    break;
                }
                break;
            case R.id.activity_activityComment:
                Intent intent1 = new Intent(DynamicActivity.this, fragment_dynamic_comment.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        intent1.putExtra("KeyConvert", KeyConvert);//给intent添加额外数据，key为“str”,key值为key3
                startActivity(intent1);
                break;
            case R.id.activity_attentionButton:
                //点击后Button动画改变
                if (attentionImageViewnum.equals("0")) {
                    Animation animation1 = AnimationUtils.loadAnimation(DynamicActivity.this, R.anim.dynamic_love_animation);
                    attentionImageView.startAnimation(animation1);//开始动画
                    attentionImageView.setImageResource(R.drawable.guanzhu2);
                    attentionImageViewnum = "1";
                    break;
                } else if (attentionImageViewnum.equals("1")) {
                    Animation animation2 = AnimationUtils.loadAnimation(DynamicActivity.this, R.anim.dynamic_love_animation1);
                    attentionImageView.startAnimation(animation2);//开始动画
                    attentionImageView.setImageResource(R.drawable.guanzhu);
                    attentionImageViewnum = "0";
                    break;
                }
            case R.id.activity_eventSharing:

                break;
        }
    }

    //CollapsingToolbarLayout展开、折叠、中间，这三种状态。
    private enum CollapsingToolbarLayoutState {
        EXPANDED,
        COLLAPSED,
        INTERNEDIATE
    }
}
